﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Model
{

    public class T
    {
        public Int16 ReadCodeCMD { get; set; }
        public Int16 ReadBeforWeightCMD { get; set; }
        public Int16 ReadAfterWeightCMD { get; set; }
        public Int16 ReadSupplementCMD { get; set; }
    }

    public class T1
    {
        public T1()
        {
            Code = new byte[30];
        }
        public bool HaveBattry { get; set; }
        public bool CodeOK { get; set; }
        public bool ShortCircuitTestOk { get; set; }
        public bool OppositeHoleOK { get; set; }
        public bool VacaummTestOk { get; set; }
        public bool ReadDataOK { get; set; }
        public byte demo1 { get; set; }
        public float BeforWeight { get; set; } 
        public Int16 BatteryNUM { get; set; }
        public byte[] Code { get; set; }
    }

    public class FixT1
    {
        public FixT1()
        {
        }
        public bool HaveBattry { get; set; }
        public bool CodeOK { get; set; }
        public bool ShortCircuitTestOk { get; set; }
        public bool OppositeHoleOK { get; set; }
        public bool VacaummTestOk { get; set; }
        public bool ReadDataOK { get; set; }
        public byte demo1 { get; set; }
        public float BeforWeight { get; set; }
        public Int16 BatteryNUM { get; set; }
        public string Code { get; set; }
    }

    public class T2
    {
        public T2()
        {
            Code = new byte[30];
        }
        public bool HaveBattry { get; set; }
        public bool Vacaumm_Test_ok { get; set; }
        public bool ReadDataOK { get; set; }
        public bool Weight_OK { get; set; } 
        public byte demo1 { get; set; }
        public float BeforWeight { get; set; }
        public float AfterWeight { get; set; }
        public float Weight { get; set; }
        public float Offset { get; set; }
        public Int16 BatteryNUM { get; set; }
        public Int16 StationNUM { get; set; }
        public Int16 Col { get; set; }
        public Int16 Line { get; set; }
        public byte[] Code { get; set; }
    }
    public class FixT2
    {
        public FixT2()
        {
        }
        public bool HaveBattry { get; set; }
        public bool Vacaumm_Test_ok { get; set; }
        public bool ReadDataOK { get; set; }
        public bool Weight_OK { get; set; } 
        public float BeforWeight { get; set; }
        public float AfterWeight { get; set; }
        public float Weight { get; set; }
        public float Offset { get; set; }
        public Int16 BatteryNUM { get; set; }
        public Int16 StationNUM { get; set; }
        public Int16 Col { get; set; }
        public Int16 Line { get; set; }
        public string Code { get; set; }
    }


    public static class Helper
    {
        public static FixT1 ConvertToFixT1(this T1 t1)
        {
            var fixT1 = new FixT1();
            fixT1.HaveBattry = t1.HaveBattry;
            fixT1.CodeOK = t1.CodeOK;
            fixT1.ShortCircuitTestOk = t1.ShortCircuitTestOk;
            fixT1.OppositeHoleOK = t1.OppositeHoleOK;
            fixT1.VacaummTestOk = t1.VacaummTestOk;
            fixT1.BeforWeight = t1.BeforWeight;
            fixT1.BatteryNUM = t1.BatteryNUM;
            fixT1.Code = ConvertByteToString(t1.Code);  

            return fixT1;
        }
        public static FixT2 ConvertToFix(this T2 t2)
        {
            var fixT2 = new FixT2();
            fixT2.HaveBattry = t2.HaveBattry;
            fixT2.Vacaumm_Test_ok = t2.Vacaumm_Test_ok;
            fixT2.ReadDataOK = t2.ReadDataOK;
            fixT2.Weight_OK = t2.Weight_OK;
            fixT2.BeforWeight = t2.BeforWeight;
            fixT2.AfterWeight = t2.AfterWeight;
            fixT2.Weight = t2.Weight;
            fixT2.Offset = t2.Offset;
            fixT2.BatteryNUM = t2.BatteryNUM;
            fixT2.StationNUM = t2.StationNUM;
            fixT2.Col = t2.Col;
            fixT2.Line = t2.Line; 
            fixT2.Code = ConvertByteToString(t2.Code);

            return fixT2;
        }
        public static string ConvertByteToString(byte[] b)
        {
            //var content= Encoding.Default.GetString(b, 2, b.Length - 2).Replace("\0", "").Trim();
            //var index = content.IndexOf("\r");
            //if (index > 0)
            //{
            //    return content.Substring(0, index);
            //}
            //return content;

            try
            {
                var len = b[1];
                var resultBytes = b.Skip(2).Take(len).ToArray();
                var content = Encoding.Default.GetString(resultBytes).Trim();
                return content;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}
